package com.example.uchain.mapper;

import com.example.uchain.demo.Trade;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface TradeMapper {

    //分页查询 未购买
    List<Trade> findAll(
            @Param("ownerName") String ownerName,
            @Param("workName") String workName,
            @Param("CRId") Integer CRId,
            @Param("workType") String workType,
            @Param("authorization") String authorization,
            @Param("blockAddress") String blockAddress,
            @Param("code") Integer code);

    //添加
    @Insert("insert into trade (ownerName, ownerContact, workName, workType, tradingTime, blockAddress, certificateFile, CRId, authorization, price, PurchaseState, FavoritesState,treadTime,isExpand,uid)"+
            " values(#{ownerName},#{ownerContact},#{workName},#{workType},#{tradingTime},#{blockAddress},#{certificateFile},#{CRId},#{authorization},#{price},#{PurchaseState},#{FavoritesState},#{treadTime},#{isExpand},#{uid})")
    void AddTrade(Trade trade);

    @Delete("delete from trade where T_id = #{TId}")
    //删除
    void deleteTradeById(Integer TId);

    //修改
    void updateById(Trade trade);

    //修改购买状态
    void updateById1(Trade trade);

    //修改收藏状态
    void updateById2(Trade trade);

    @Select("select * from trade where CRId =#{CRId}")
    List<Trade> findAll1(Integer CRId);

    @Select("select * from trade where T_id =#{TId}")
    List<Trade> findAllByTid(Integer TId);
    //分页查询 已收藏
    List<Trade> findAll2(
            @Param("ownerName") String ownerName,
            @Param("workName") String workName,
            @Param("CRId") Integer CRId,
            @Param("workType") String workType,
            @Param("authorization") String authorization,
            @Param("blockAddress") String blockAddress,
            @Param("code") Integer code);

    //分页查询 已收藏 未购买
    List<Trade> findAll3(
            @Param("ownerName") String ownerName,
            @Param("workName") String workName,
            @Param("CRId") Integer CRId,
            @Param("workType") String workType,
            @Param("authorization") String authorization,
            @Param("blockAddress") String blockAddress,
            @Param("code") Integer code,
            @Param("uid") Integer uid);

    //分页查询 已收藏 已购买
    List<Trade> findAll4(
            @Param("ownerName") String ownerName,
            @Param("workName") String workName,
            @Param("CRId") Integer CRId,
            @Param("workType") String workType,
            @Param("authorization") String authorization,
            @Param("blockAddress") String blockAddress,
            @Param("code") Integer code,
            @Param("uid") Integer uid);

    //分页查询  已购买
    List<Trade> findAll5(
            @Param("ownerName") String ownerName,
            @Param("workName") String workName,
            @Param("CRId") Integer CRId,
            @Param("workType") String workType,
            @Param("authorization") String authorization,
            @Param("blockAddress") String blockAddress,
            @Param("code") Integer code,
            @Param("uid") Integer uid);

    //修改收藏状态
    void updateById3(Trade trade);

    //查询热门权益人
    @Select("SELECT ownerName,ownerContact, COUNT(ownerName) AS nameCount\n" +
            "FROM trade\n" +
            "GROUP BY ownerName,ownerContact\n" +
            "ORDER BY nameCount DESC\n" +
            "LIMIT 7")
    List<Trade> findAllPeople();

//    根据uid查询交易信息
    @Select("select * from trade where uid =#{uid}")
    List<Trade> tradeFindByUid(Integer uid);


    @Results(id = "TradeCountByDayResultMap", value = {
            @Result(column = "date", property = "date"),
            @Result(column = "new_trade_count", property = "count")
    })
    @Select("SELECT DATE(treadTime) AS date, COUNT(*) AS new_trade_count " +
            "FROM trade " +
            "WHERE treadTime >= DATE_SUB(CURDATE(), INTERVAL 9 DAY) " +
            "GROUP BY DATE(treadTime) " +
            "ORDER BY DATE(treadTime)")
  List<Map<String, Object>> newTradeCountByDay();

    @Select("SELECT uid, COUNT(*) as occurrence_count "
            + "FROM trade "
            + "GROUP BY uid "
            + "ORDER BY occurrence_count ASC  "
            + "LIMIT 10")
    List<Map<String, Object>> OrderAndCount();
}
